home *** CD-ROM | disk | FTP | other *** search
- Path: library.erc.clarkson.edu!rpi!not-for-mail
- From: floydb1@lib108.its.rpi.edu (Barry B Floyd)
- Newsgroups: comp.lang.c++
- Subject: Re: Creating a pointer to a function "void (*ptrFunction)()" inside a class
- Date: 8 Jan 1996 14:22:59 -0500
- Organization: Rensselaer Polytechnic Institute, Troy, NY.
- Message-ID: <4crquj$7t1@lib108.its.rpi.edu>
- References: <30ECA10F.3D99@ifu.net> <NITIN.96Jan5125830@more.eng.sun.com>
- NNTP-Posting-Host: lib108.its.rpi.edu
- X-newsreader: xrn 7.04-beta-11
-
-
- In article <NITIN.96Jan5125830@more.eng.sun.com>, nitin@more.eng.sun.com (Nitin More [CONTRACTOR]) writes:
- |> In article <30ECA10F.3D99@ifu.net> Jason Gresh <gresh@ifu.net> writes:
- |> > class BaseClass
- |> > {
- |> > int (*ptrFunction)();
- |> > }
- |> >
- |> > class DerivedClass : BaseClass
- |> > {
- |> > DerivedClass::DerivedClass();
- |> > int myFunction(int, int);
- |> > }
- |> >
- |> > DerivedClass::DerivedClass()
- |> > {
- |> > ptrFunction = myFunction;
- |> > }
- |> >
- |> > DerivedClass::myFunction(int, int)
- |> > {
- |> > // code ....
- |> > }
- |> >
-
- Try:
-
- class BaseClass
- {
- ... // other stuff
- virtual int Function ( int, int ) = 0 ; // pure virtual fn()
- ... // other stuff
- }
-
- class DerivedClass : BaseClass
- {
- ... // other stuff
- int Function ( int, int ) { // your code } ;
- ... // other stuff
- }
-
- If DerivedClass does not declare "Function ( int, int )" you will
- get a compiler error. All classes derived from BaseClass will
- essentially be required to provide a "Function ( int, int )".
- However, I believe you will NOT be able to create an instance of
- BaseClass (i.e. BaseClass a_base ; ) because of the pure virtual
- function. This should be OK given your initial request.
-
- Generally, the "= 0" keeps you honest, especially useful with derived
- classes containing many many functions or containing functions with
- the same name but differing parameter lists and/or return values.
-
- barry
-
- ps
-
- note: "Function ( int, int )" remains virtual in DerivedClass,
- even though the keyword "virtual" is not included in the declaration.
- Thus, I believe "class DerivedDerivedClass : DerivedClass" must provide
- a "Function ( int, int )" as well.
- --
- +--------------------------------------------------------------------+
- | Barry B. Floyd \\\ floydb1@rpi.edu |
- | RPI Alum. '84 '87 '88 \\\ |
- +--------------------------------------------------------------------+
-